﻿window.SearchKey = null;
$.MGJ.RawCode = [];

var allotId = 0;

(function () {
    var _this = $.MGJ.RawCode;
    $.extend($.MGJ.RawCode, {
        Execls: function (ele) {
            $("input.AllClickChild:checked").each(function () {
                info.push($(this).attr('value'));

            });
            
            if (info.length == 0) {
                $.L.msgError("请选择要导出的数据信息！");
                return
            }
           
            if (!$(ele).find('.no-result').hasClass('no-result')) {
                window.open(ACTION_URL("Execls", "CodeManage") + "?Key=" +info);
            }
            else {
                $.L.msgWarning('导出数据不能为空！')
            }
        }
    });
})(jQuery);

(function () {
    var _this = $.MGJ.RawCode;
    $.extend($.MGJ.RawCode, {
        //详细数据
        Dialog_RawShowDataObj: null,
        Dialog_RawShowData: function (obj) {
            var id = $(obj).attr("ID");
            this.Dialog_RawShowDataObj = $.L.dialog({
                url: ACTION_URL("RawShowData", "CodeManage") + "?id=" + id,
                title: "详细数据",
                width: 1100,
                height: "auto",
                buttons: {
                    "button_cancel": {
                        type: 'default',
                        text: '取消',
                        click: 'MODALMISS'
                    }
                }
            });
        },
        // 新增生码任务
        Dialog_BornCodeObj: null,
       
        Dialog_BornCode: function (allotid, EID, Count, CodeTypes) {
        
            this.Dialog_BornCodeObj = $.L.dialog({
                url: ACTION_URL("BornCode", "CodeManage"),
                title: "新增生码任务",
                width: 800,
                height: 'auto',
                buttons: {
                    "button_save": {
                        type: 'primary',
                        text: '保存',
                        click: function () {
                            var bootstrapValidator = $("#form_BornCode").data('bootstrapValidator').validate();//触发全部验证
                            if (bootstrapValidator.isValid()) {
                                _this.Ajax_AddRowCode(allotid);
                            }
                            else {
                                $.L.msgError("请检查数据格式！");
                            };
                        }
                    },
                    "button_cancel": {
                        type: 'default',
                        text: '取消',
                        click: 'MODALMISS'
                    }
                }
            });

            //setTimeout(function ()
            //{
            //    if (allotid != null && EID != null && Count != null) {
            //    $("#Eid").val(EID);
            //    $("#Gen_Code_Nums").attr("value", Count);
            //    $('#EnterpriseId option').each(function (i, ele) {
            //        var val = $(ele).attr('id');
            //        if (EID == val) {                      
            //            $(ele).attr('selected', 'selected');
            //            ChangeValue($(ele));
            //            function ChangeValue(ele) {
            //                var ChangeValue = $(ele).text();
            //                $(".value").val('您好！您所查询的是' + ChangeValue + '生产的品牌产品，是正牌产品的标识，欢迎您再次选购！');
            //                $(".value").attr("title", '您好！您所查询的是' + ChangeValue + '生产的品牌产品，是正牌产品的标识，欢迎您再次选购！');
            //            }
            //            _this.Ajax_GetBatch(EID);
            //        }
            //    })
            //    }
            //    if (CodeTypes != null) {
            //        arr = CodeTypes.split(',');              
            //    if (arr.length == 1) {
            //        if (arr[0] == 1)
            //        { $('.CodeTypes[value=1]').attr('checked', true) }
            //        else if (arr[0] == 2) {
            //            $('.CodeTypes[value=2]').attr('checked', true)
            //        }
            //    }
            //    else {
            //        $('.CodeTypes').attr('checked', true)
            //    }
            //    }
               
            //}, 1200);

        },
        Dialog_EditCodeObj: null,
        Dialog_EditCode: function (obj) {
            var id = $(obj).attr("ID");
            
            this.Dialog_EditCodeObj = $.L.dialog({
                url: ACTION_URL("EditCode", "CodeManage") + "?id=" + id,
                title: "编辑",
                width: 800,
                height: "auto",
                buttons: {
                    "button_save": {
                        type: 'primary',
                        text: '保存',
                        click: function () {
                            //var bootstrapValidator = $("#Product_form").data('bootstrapValidator');
                            //bootstrapValidator.validate();
                            //if (bootstrapValidator.isValid()) {
                            $.MGJ.RawCode.Ajax_EditRowCode();
                            //}
                            //else {
                            //    $.L.msgError("请检查数据格式！");
                            //};
                        }
                    },
                    "button_cancel": {
                        type: 'default',
                        text: '取消',
                        click: 'MODALMISS'

                    }
                }
            });
        }

    });
})(jQuery);
(function () {
    var _index = 1;
    var _this = $.MGJ.RawCode;
    $.extend($.MGJ.RawCode, {
        //多条件查询
        Ajax_GetRowCode: function (pageIndex) {
            window.SearchKey = removeSpace($("#EnterpriseName").val());
            _index = pageIndex;
            $.L.ajaxHandle({
                url: ACTION_URL("GetRowCode", "CodeManage") + "?pageindex=" + pageIndex,
                data: $("#SearchForm").serialize(),
                success: function (response) {
                    $("#RowCodeManage").L_bindingDataList(response);
                }
            });
        },
      
        Ajax_AddRowCode: function (allotId) {
            var types = [];
            $('input[name="CodeTypes"]:checked').each(function (i, e) {
                types.push($(this).val())
            });
            $("#CodeType").val(types.join(','));
            var Batch = $("#Batch").val();
            if (allotId == undefined || allotId == null)
            {
                allotId = 0;
            }
            
            $.L.ajaxHandle({
                url: ACTION_URL("AddRowCode", "CodeManage") + "?Batch=" + Batch + "&allotId=" + allotId,
                data: $("#form_BornCode").serialize(),
                success: function (response) {
                    _this.Dialog_BornCodeObj.close();
                    if (response) {
                        _this.SetAllotPass(allotId);
                        _this.Ajax_GetRowCode(1);
                        //NewsRoundRobin(1, 1000000); //生码
                        $.L.msgSuccess("操作成功");
                        
                        $.MGJ.AllotCode.GetCurrentUserAllotList(1);
                    
                    }
                            
                }
            });
        }
,
    
        SetAllotPass:function(allotId){
        
            if (allotId!=undefined){
            $.L.ajaxHandle({
                url: ACTION_URL("SetAllotPass", "CodeManage"),
                data: { allotId: allotId },
                success: function (response) {
                    if (response == 1) {
                        $.MGJ.AllotCode.GetCurrentUserAllotList();
                    }


                }
           
            })
            }
        }
,


        codebocx: function () {
            $("input[name='CodeTypes']").each(function () {
                if ($(this).is(':checked')) {
                    var val = $("#CodeType").val() + $(this).val();

                    function quchong(str) {
                        var newStr = "";
                        for (var i = 0; i < str.length; i++) {
                            if (newStr.search(str[i]) == -1)
                                newStr += str[i] + ',';

                        }
                        return newStr.substr(0, newStr.length - 1);
                    }

                   


                    $("#CodeType").val(quchong(val))
                }
            })
            if ($("#CodeType").val().substr(0, 1) == ',') {
                $("#CodeType").val($("#CodeType").val().substr(1))
            }
        },


        Ajax_GetBatch: function (eid) {
            $.L.ajaxHandle({
                url: ACTION_URL("GetCodBatch", "CodeManage") + "?EnterpriseId=" + eid,
                success: function (response) {
                    $("#Batch").val(response);
                    $('#form_BornCode').data('bootstrapValidator')
                        .updateStatus('Batch', 'NOT_VALIDATED', null)
                        .validateField('Batch');
                }
            });
        },
       
        Ajax_handers: function (obj) {
            var temp = obj.split(",");
            var result = "";
            for (i = 0; i < temp.length ; i++) {
                if (temp[i] == 1) {
                    result += " 服务码 ";
                } else if (temp[i] == 2) {
                    result += " 管理码 ";
                } else if (temp[i] == 3) {
                    result += " 序列号 ";
                }
            }
            return result;
        },
        Ajax_InitStatus: function (obj) {
            return obj == 0 ? "待生码" : "生码成功";
        },
        Ajax_ViewFlag: function (obj) {
            return obj == 0 ? "禁用" : "启用";
        },
        //删除
        Ajax_DelRowCode: function (obj) {
            $.L.msgConfirm("确认删除该记录?", function () {
                var id = $(obj).attr("ID");
                $.L.ajaxHandle({
                    url: ACTION_URL("DelRowCode", "CodeManage") + "?id=" + id,
                    data: { id: id },
                    success: function (response) {
                        if (response > 0) {
                            $.L.msgSuccess("操作成功！");
                            _this.Ajax_GetRowCode(_index);
                        }
                    }
                });
            })

        },
        //修改
        Ajax_EditRowCode: function (data) {

            $.L.ajaxHandle({
                url: ACTION_URL("EditRowCode", "CodeManage"),
                data: $("#form_editCode").serialize(),
                success: function (response) {
                    if (response > 0) {
                        _this.Dialog_EditCodeObj.close();
                        $.L.msgSuccess("操作成功！");
                        _this.Ajax_GetRowCode(_index);
                    }
                }
            });
        },




        //下载
        //Ajax_ExportCode: function (obj) {
        //    var PatchId = $(obj).attr("PatchId");
        //    var code = $(obj).attr("code");
        //    var postfix = code + PatchId;
        //    $.ajax({
        //        url: ACTION_URL("ExistsCodeTable", "CodeManage") + "?pid=" + PatchId + "&code=" + code,
        //        success: function (response) {                
        //            if (response == "False") {
        //                $.L.msgError("请检查该码状态是否是待生码或者该码是否禁用！");
        //            }
        //            else {
        //                window.open("/CodeManage/ExportCode?productBatchId=" + PatchId + "&code=" + code);
        //            }
        //        }
        //    });
        //},

        Ajax_ExportCodeObj : null,
        Ajax_ExportCode: function (obj) {
            var PatchId = $(obj).attr("PatchId");
            var code = $(obj).attr("code");
            var postfix = code + PatchId;
            var CodeType = $(obj).attr("CodeType");
            var that = this;
            
            $.ajax({
                url: ACTION_URL("ExistsCodeTable", "CodeManage") + "?pid=" + PatchId + "&code=" + code,
                success: function (response) {
                    if (response == false) {
                        $.L.msgError("请检查该码状态是否是待生码或者该码是否禁用！");
                    }
                    else if (CodeType) {
                     
                        var str = CodeType;
                        if (str.indexOf("3") != -1) {
                           
                            var id = $(obj).attr("ID");
                            that.Ajax_ExportCodeObj = $.L.dialog({
                                url: ACTION_URL("downloadCode", "CodeManage") + "?id=" + id,
                                title: "下载序列码",
                                width: 800,
                                height: "auto",
                                buttons: {
                                    "button_save": {
                                        type: 'primary',
                                        text: '确定',
                                        click: function () {
                                            var bootstrapValidator = $("#form_downloadCode").data('bootstrapValidator').validate();//触发全部验证
                                            if (bootstrapValidator.isValid()) {
                                                
                                                var englishnum = $("#serialNum").val()
                                             
                                                var num = $("#startCode").val()
                                              
                                              
                                                
                                                _this.Ajax_ExportCodeObj.close();
                                                window.open("/CodeManage/ExportCode?productBatchId=" + PatchId + "&code=" + code + "&englishnum=" + englishnum + "&num=" + num);
                                               
                                            }
                                            else {
                                                $.L.msgError("请检查数据格式！");
                                            };
                                        }
                                    }
                                },
                                "button_cancel": {
                                    type: 'default',
                                    text: '取消'
                                    //click: 'MODALMISS'

                                }
                            });
                        }
                        else {                          
                            window.open("/CodeManage/ExportCode?productBatchId=" + PatchId + "&code=" + code);
                        }
                    }
                }
                    
            });
        
        },







        //启用禁用
        Ajax_GetViewFlag: function (flge) {
            var CheckValue = "";
            var CheckFlag = "";
            $('input[name="AllClickChild"]:checked').each(function () {
                CheckValue += "," + $(this).val();
                CheckFlag += "," + $(this).attr("flag");
            });

            checkValue_ = CheckValue.substring(1);
            CheckValueArr = CheckValue.substring(1).split(",");
            CheckFlagArr = CheckFlag.substring(1).split(",");
            if (CheckValue == "") {
                $.L.msgError("请先选中一项！");
            } else {
                var CheckFlagArrAdd = 0;
                for (var i = 0; i < CheckFlagArr.length; i++) {
                    CheckFlagArrAdd += parseInt(CheckFlagArr[i]);
                }
                var str = "";
                if (flge == 0) {
                    if (CheckFlagArrAdd != 0) {
                        str = "禁用";
                        $.L.msgConfirm("确定" + str + "吗？", function () {
                            $.L.ajaxHandle({
                                url: ACTION_URL("GetViewFlag", "CodeManage"),
                                data: { flge: flge, CheckValue: checkValue_ },
                                success: function (response) {
                                    if (response > 0) {
                                        $.MGJ.RawCode.Ajax_GetRowCode(1);
                                        //$('input[name="AllClickChild"]:checked').each(function () {
                                        //    $(this).attr("flag", "0")
                                        //});
                                        $.L.msgSuccess("操作成功");
                                    }
                                }
                            });
                        })
                    } else {
                        $.L.msgError('已禁用！');
                    }
                } else {
                    str = "启用";
                    if (CheckFlagArrAdd != CheckFlagArr.length) {
                        $.L.msgConfirm("确定" + str + "吗？", function () {
                            $.L.ajaxHandle({
                                url: ACTION_URL("GetViewFlag", "CodeManage"),
                                data: { flge: flge, CheckValue: checkValue_ },
                                success: function (response) {
                                    if (response > 0) {
                                        $.MGJ.RawCode.Ajax_GetRowCode(1);
                                        //$('input[name="AllClickChild"]:checked').each(function () {
                                        //    $(this).attr("flag", "1")
                                        //});
                                        $.L.msgSuccess("操作成功");
                                    }
                                }
                            });
                        })
                    }
                    else {
                        $.L.msgError('已启用！');
                    }
                }
            }







        },
        Ajax_PoductNameList: function (eid, ProductId) {
            $.L.ajaxHandle({
                url: ACTION_URL("SelnameList", "CodeManage") + "?Eid=" + eid,
                success: function (response) {
                
                    $("#ProductId").html("");
                    var $option = response.length == 0 ? "<option value='0' selected >请先添加产品信息</option>" : "<option value='0' selected >--请选择--</option>";              
                    $(response).each(function (i, d) {
                        
                            $option+= d.Id == ProductId
                                ?
                           "<option value='" + d.Id + "' selected >" + d.ProductName + "</option>"
                                :
                           "<option value='" + d.Id + "'>" + d.ProductName + "</option>";
                           
                        });
                        $("#ProductId").append($option);
                    }
                
            });
        },
        Ajax_ProductTypeList: function (Pid) {
            $.L.ajaxHandle({
                url: ACTION_URL("SeltypeList", "CodeManage") + "?Pid=" + Pid,
                success: function (response) {
                    $("#TypeId").html("");
                    var $option = response.length == 0 ? "<option value='0' selected >请先添加产品信息</option>" : "<option value='0' selected >--请选择--</option>";
                    $(response).each(function (i, d) {
                        $option = d.Id== TypeId
                            ?
                       "<option value='" + d.Id + "' selected >" + d.TypeName + "</option>"
                            :
                       "<option value='" + d.Id + "'>" + d.TypeName + "</option>";
                    });
                    $("#TypeId").html($option);
                }
            });
        },
        Ajax_BatchList: function (Pid) { 
            $.L.ajaxHandle({
                url: ACTION_URL("SelBatchList", "CodeManage") + "?Pid=" + Pid,
                success: function (response) {
                    $("#ProductBatchId").html("");
                    var $option = response.length == 0 ? "<option value='0' selected >请先添加产品信息</option>" : "<option value='0' selected >--请选择--</option>";
                        $(response).each(function (i, d) {
                            $option = d.Id == ProductBatchId
                                ?
                           "<option value='" + d.Id + "' selected >" + d.BatchName + "</option>"
                                :
                           "<option value='" + d.Id + "'>" + d.BatchName + "</option>";
                            $("#ProductBatchId").append($option);
                        });        
                   
                }
            });
        },


    });

})(jQuery);

